package com.googlecalendar;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventAttendee;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.googlelogin.GoogleAuthHelper;






import com.propertypoint.presentation.model.GoogleUser;
import com.propertypoint.util.PropertyPointConstants;

import java.util.ArrayList;

/**
 * Servlet implementation class CreateCalenderEventServlet
 */
@WebServlet("/CreateCalenderEventServlet")
public class CreateCalenderEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateCalenderEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		GoogleUser googleUser = (GoogleUser)request.getSession().getAttribute("googleUser");
		GoogleAuthHelper helper = new GoogleAuthHelper();
		if(null == googleUser){
			
			googleUser = helper.getUserInfoJson();
		}
		if(null != googleUser){
			System.out.println(googleUser + "  Test ServletFileUpload");
			
			String code = (String)request.getSession().getAttribute("googleCode");
			
			helper.createEvent(code);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(
				request.getInputStream()));
		String json = "";
		if (br != null) {
			json = br.readLine();

		}
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		response.setHeader("Cache-control", "no-cache, no-store");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "-1");

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		response.setHeader("Access-Control-Allow-Headers", "Content-Type");
		response.setHeader("Access-Control-Max-Age", "86400");
		
		GoogleUser googleUser = (GoogleUser)request.getSession().getAttribute("googleUser");
		GoogleAuthHelper helper = new GoogleAuthHelper();
		if(null == googleUser){
			
			googleUser = helper.getUserInfoJson();
		}
		
		JsonParser paser = new JsonParser();
		JsonElement element = paser.parse(json);
		JsonObject jsonObject = element.getAsJsonObject();
		JsonElement elementeventsummary = jsonObject
				.get(PropertyPointConstants.EVENT_SUMMARY);
		JsonElement elementeventDescription = jsonObject
				.get(PropertyPointConstants.EVENT_DESCRIPTION);
		JsonElement elementCustomerEmail = jsonObject
				.get(PropertyPointConstants.CUSTOMER_EMAIL);
		JsonElement elementMeetingPoint = jsonObject
				.get(PropertyPointConstants.MEETING_POINT);
		JsonElement elementDateTime = jsonObject
				.get(PropertyPointConstants.DATE_TIME);
		//dateTime
		String eventsummary = elementeventsummary.getAsString();
		String eventDescription = elementeventDescription.getAsString();
		String customerEmail = elementCustomerEmail.getAsString();
		String meetingPoint = elementMeetingPoint.getAsString();
		String dateTime = elementDateTime.getAsString();
		java.util.Date appointmentDate = null;
		try {
			appointmentDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(dateTime);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//2014-12-17 09:50:00
		
		try{
		if(null == appointmentDate){
			
			appointmentDate = new Date(new Date().getTime()+ 360000000);
		}
		helper.createEvent(eventsummary, eventDescription, customerEmail, meetingPoint, appointmentDate, googleUser);
		}catch(Exception e){
			e.printStackTrace();
			
		}
		request.setAttribute("event_status", "true");
		request.setAttribute("event_message", "Event Created !");
		out.println("Appointment successfully created!");
		out.close();
		//eventsummary
		//eventDescription
		//customerEmail
		
		
		
		// TODO Auto-generated method stub
	}

}
